﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Dude.MLS
{
    internal class LocaleModule
    {
        private static Dictionary<string, LocaleModule> loadedModules = new Dictionary<string, LocaleModule>();

        private SortedDictionary<string, string> dictionary;

        public LocaleModule(string langCode, string module)
        {
            dictionary = null;

            Load(langCode, module);

            loadedModules[GetModuleKey(langCode, module)] = this;
        }

        private bool Load(string langCode, string module)
        {
            try
            {
                if (module == null || module == String.Empty)
                    module = "common";

                string filename = String.Format("{0}Lang\\{2}.{1}.xml", AppDomain.CurrentDomain.BaseDirectory, langCode, module);

                FileInfo fi = new FileInfo(filename);
                if (!fi.Exists)
                    return false;

                dictionary = new SortedDictionary<string, string>();
                using (FileStream fstream = fi.OpenRead())
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.IgnoreComments = true;
                    settings.IgnoreProcessingInstructions = true;
                    settings.IgnoreWhitespace = true;
                    XmlReader xreader = XmlReader.Create(fstream, settings);

                    string key = null;
                    string text = null;

                    bool el_i = false;
                    bool el_o = false;
                    
                    while (xreader.Read())
                    {
                        switch (xreader.NodeType)
                        {
                            case XmlNodeType.Element:
                                switch(xreader.LocalName)
                                {
                                    case "t":
                                        el_i = false;
                                        el_o = false;
                                        break;
                                    case "i":
                                        el_i = true;
                                        break;
                                    case "o":
                                        el_o = true;
                                        break;
                                }
                                break;
                            case XmlNodeType.Text:
                                if (el_i) key = xreader.Value;
                                if (el_o) text = xreader.Value;
                                break;
                            case XmlNodeType.EndElement:
                                if (el_o && (key != null) && (text != null) && (key != "") && (text != ""))
                                {
                                    dictionary[key] = text;
                                    key = null;
                                    text = null;
                                }
                                el_i = el_o = false;
                                break;
                        }
                    }
                }

                return true;
            }
            catch (Exception)
            {
                dictionary = null;
                return false;
            }
        }

        internal static LocaleModule GetModule(string langCode, string module)
        {
            try
            {
                return loadedModules[GetModuleKey(langCode, module)];
            }
            catch (KeyNotFoundException)
            {
                return new LocaleModule(langCode, module);
            }
        }

        private static string GetModuleKey(string langCode, string module)
        {
            if (module == null || module == String.Empty)
                module = "main";

            return langCode + ":" + module;
        }

        public string GetText(string key)
        {
            if ((key == null) || (key == ""))
                return "";

            if (dictionary == null)
                return key;
            try
            {
                return dictionary[key];
            }
            catch (KeyNotFoundException)
            {
                return key;
            }
        }
    }
}
